package com.hertz.alarm.util;

import java.util.List;

import org.codehaus.jackson.annotate.JsonIgnore;

public class Page<T> {
	private int totalCount;
	private int page;
	private int count;
	private List<T> result;
	private int begin;
	private int end;
	private int step;
	private int totalPage;
	private boolean hasFirst;
	private boolean hasFinal;

	public Page() {

	}

	public Page(int totalCount, int page, int count, List<T> result,
			int step) {
		this.totalCount = totalCount;
		this.page = page;
		this.count = count;
		this.result = result;
		this.step = step;
		setTotalPage();
		setBegin();
		setEnd();
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin() {
		begin = page - (int) ((step - 1) / 2);
		if (begin <= 0) {
			begin = 1;
		}
	}

	public int getEnd() {

		return end;
	}

	public void setEnd() {
		end = page + (int) ((step - 1) / 2);
		if (end < step) {
			end = step;
		}
		if (end > totalPage) {
			end = totalPage;
		}
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage() {
		totalPage = (totalCount / count) + (totalCount % count > 0 ? 1 : 0);
	}
	@JsonIgnore
	public boolean isHasFirst() {
		if (begin == 1) {
			hasFirst = false;
		} else {
			hasFirst = page > (step / 2);
		}
		return hasFirst;
	}
	@JsonIgnore
	public boolean isHasFinal() {
		if (end == totalPage) {
			hasFinal = false;
		} else {
			hasFinal = page < totalPage;
		}
		return hasFinal;
	}

}